﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace StairsModuleAndEdit.StairsEdit.Common
{
	internal class CommonUnit
	{
		public static bool GetAllSameViewPlanByActiveView(ExternalCommandData m_commandData, ref IList<ViewPlan> viewList, ref ViewPlan topPlan)
		{
			try
			{
				UIDocument activeUIDocument = m_commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				viewList = new List<ViewPlan>();
				IList<Element> list = new List<Element>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				list = filteredElementCollector.OfClass(typeof(ViewPlan)).WhereElementIsNotElementType().ToElements();
				View activeView = activeUIDocument.ActiveView;
				topPlan = (activeView as ViewPlan);
				string a = activeView.GetParameter("视图分类-父").AsString();
				foreach (Element element in list)
				{
					ViewPlan viewPlan = element as ViewPlan;
					if (viewPlan != null && viewPlan.GetParameter("视图分类-父") != null && viewPlan.GenLevel != null && viewPlan.ViewType == activeView.ViewType && a == viewPlan.GetParameter("视图分类-父").AsString())
					{
						viewList.Add(viewPlan);
						if (viewPlan.GenLevel.Elevation > topPlan.GenLevel.Elevation)
						{
							topPlan = viewPlan;
						}
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

        public static void GetElementType<T>(Document document, string strSymbolName, ref T newType, ref bool isNew) where T : Autodesk.Revit.DB.ElementType
		{
			isNew = true;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfClass(typeof(T));
			foreach (Element element in filteredElementCollector)
			{
				T t = element as T;
				if (t != null)
				{
					string name = t.Name;
					if (name.CompareTo(strSymbolName) == 0)
					{
						isNew = false;
						newType = t;
						break;
					}
				}
			}
		}

		public static void CorrectYJKStairsTypeByNumber(Autodesk.Revit.DB.Document doc, ref string strName)
		{
			List<int> list = new List<int>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(StairsType));
			foreach (Element element in filteredElementCollector)
			{
				StairsType stairsType = element as StairsType;
				if (stairsType != null)
				{
					string name = stairsType.Name;
					if (name.StartsWith(strName))
					{
						int num = name.LastIndexOf("-");
						if (num != -1)
						{
							string text = name.Substring(0, num);
							if (text.CompareTo(strName) == 0)
							{
								string value = name.Substring(num + 1);
								try
								{
									int item = Convert.ToInt32(value);
									list.Add(item);
								}
								catch (Exception)
								{
								}
							}
						}
					}
				}
			}
			int num2 = 1;
			if (list.Count > 0)
			{
				list.Sort();
				num2 = list[list.Count - 1];
				num2++;
			}
			strName = strName + "-" + num2.ToString();
		}

		public static double MM2Feet = 304.8;

		public static double _epsDouble = 1E-09;

		public static double _epsRiseHeight = 10.0 / CommonUnit.MM2Feet;
	}
}
